import 'package:any_reader/public.dart';

import '../provider/export.dart';
import 'export.dart';

class BookInfoModel extends ViewStateModel {
  ThemeModel themeModel;
  BookInfoModel(this.themeModel);
  String title;
  String url;
  String author;
  String now;
  String reptileType;
  String reptileType1;
  BookInfo bookInfo = new BookInfo();
  initData({
    String title,
    String url,
    String author,
    String now,
    String reptileType,
    String reptileType1,
  }) async {
    this.title = title;
    this.url = url;
    this.author = author;
    this.now = now;
    this.reptileType = reptileType;
    this.reptileType1 = reptileType1;
    bookInfo = new BookInfo(
        book: new Book(
            title: title, author: author, description: "暂无简介", imgUrl: ""),
        list: []);
    setBusy(true);
    bookInfo = await BookInfoService.get(
        this.title, this.url, this.author, this.reptileType);
    setBusy(false);
    notifyListeners();

    Future.delayed(Duration(milliseconds: 0), () {
      DatabaseHelper().insertChapter(
          bookInfo.book.bookId, bookInfo.book.reptileType, bookInfo.list);
    });
  }

  Chapter findChapter() {
    Chapter chapter;
    if (int.parse(now) > 0 && bookInfo.list.length >= int.parse(now)) {
      chapter = bookInfo.list[int.parse(now)];
    } else {
      chapter = bookInfo.list[0];
    }
    return chapter;
  }
}
